package com.huobao.yunying.adapters;

import java.util.Map;

import org.simple.eventbus.EventBus;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huobao.shangjia_version.activitys.Act_Store_dingdan_detail;
import com.huobao.yunying.activitys.Act_Tuikuan_detail;
import com.huobao.yunying.activitys.Act_WebView;
import com.huobao.yunying.entity.StoreSingDanData;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.DialogUtils;
import com.huobao.yunying.utils.DialogUtils.onDialogDiamissListener;
import com.huobao.yunying.utils.OpenBrowUtils;
import com.huobao.yunying.utils.UserUtils;
import com.huobao.yunying.views.NoScrollListView;
import com.huobao.zhangying.R;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import joanzapata.android.BaseAdapterHelper;
import joanzapata.android.QuickAdapter;

public class StoreDingdanManagerAdapter extends QuickAdapter<StoreSingDanData> {

	private String type;
	public StoreDingdanManagerAdapter(Context context, String type) {
		super(context, R.layout.item_store_dingdan_manager);
		this.type = type;
	}

	@Override
	protected void convert(final BaseAdapterHelper arg0, final StoreSingDanData arg1) {
		final CheckBox checkbox = arg0.getView(R.id.checkbox);
		TextView tv_dingdanhao = arg0.getView(R.id.tv_dingdanhao);
		TextView tv_status = arg0.getView(R.id.tv_status);
		tv_dingdanhao.setText("订单号：" + arg1.order_sn);
		NoScrollListView list = arg0.getView(R.id.listview);
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent;
//				if (type == null) {
//					intent = new Intent(context, Act_Tuikuan_detail.class);
//					intent.putExtra("id", tuihuo_adapter.getItem(position).order_id);
//					context.startActivity(intent);
//					return;
//				}
				intent = new Intent(context, Act_Store_dingdan_detail.class);
				intent.putExtra("order_id", getItem(position).order_id);
				context.startActivity(intent);
			}
		});
		Adapter_Store_Dingdan_item_item adapter = new Adapter_Store_Dingdan_item_item(context, arg1);
		list.setAdapter(adapter);
		// 调整费用
		TextView tv_diaozhengfeiyong = arg0.getView(R.id.tv_diaozhengfeiyong);
		// 收到货款
		TextView tv_shoudaohuokuan = arg0.getView(R.id.tv_shoudaohuokuan);
		// 查看物流
		TextView tv_wuliu = arg0.getView(R.id.tv_wuliu);
		// 取消订单
		TextView tv_quxiao_dingdan = arg0.getView(R.id.tv_quxiao_dingdan);
		// 修改单号
		TextView tv_danhao = arg0.getView(R.id.tv_danhao);
		// 发货
		TextView tv_fahuo = arg0.getView(R.id.tv_fahuo);

		int status = Integer.parseInt(arg1.status);
		switch (status) {
		// [0已取消 11待付款 10已提交 20待发货 30已发货 40已完成]
		case 0:
			tv_status.setText("状态：已取消");
			tv_diaozhengfeiyong.setVisibility(View.GONE);
			tv_shoudaohuokuan.setVisibility(View.GONE);
			tv_wuliu.setVisibility(View.GONE);
			tv_quxiao_dingdan.setVisibility(View.GONE);
			tv_danhao.setVisibility(View.GONE);
			tv_fahuo.setVisibility(View.GONE);
			checkbox.setEnabled(false);
			break;
		case 11:
			tv_status.setText("状态：待付款");
			tv_diaozhengfeiyong.setVisibility(View.VISIBLE);
			tv_shoudaohuokuan.setVisibility(View.VISIBLE);
			tv_wuliu.setVisibility(View.GONE);
			tv_quxiao_dingdan.setVisibility(View.VISIBLE);
			tv_danhao.setVisibility(View.GONE);
			tv_fahuo.setVisibility(View.GONE);
			checkbox.setEnabled(true);
			break;
		case 10:
			// tv_status.setText("状态：已提交 ");
			// tv_diaozhengfeiyong.setVisibility(View.);
			// tv_shoudaohuokuan.setVisibility(View.);
			// tv_wuliu.setVisibility(View.);
			// tv_quxiao_dingdan.setVisibility(View.);
			// tv_danhao.setVisibility(View.);
			// checkbox.setEnabled(enabled);
			break;
		case 20:
			tv_status.setText("状态：待发货");
			tv_diaozhengfeiyong.setVisibility(View.GONE);
			tv_shoudaohuokuan.setVisibility(View.GONE);
			tv_wuliu.setVisibility(View.GONE);
			tv_quxiao_dingdan.setVisibility(View.VISIBLE);
			tv_danhao.setVisibility(View.GONE);
			tv_fahuo.setVisibility(View.VISIBLE);
			checkbox.setEnabled(true);
			break;
		case 30:
			tv_status.setText("状态：已发货");
			tv_diaozhengfeiyong.setVisibility(View.GONE);
			tv_shoudaohuokuan.setVisibility(View.GONE);
			tv_wuliu.setVisibility(View.VISIBLE);
			tv_quxiao_dingdan.setVisibility(View.VISIBLE);
			tv_danhao.setVisibility(View.VISIBLE);
			tv_fahuo.setVisibility(View.GONE);
			checkbox.setEnabled(true);
			break;
		case 40:
			tv_status.setText("状态：已完成");
			tv_diaozhengfeiyong.setVisibility(View.GONE);
			tv_shoudaohuokuan.setVisibility(View.GONE);
			tv_wuliu.setVisibility(View.GONE);
			tv_quxiao_dingdan.setVisibility(View.GONE);
			tv_danhao.setVisibility(View.GONE);
			tv_fahuo.setVisibility(View.GONE);
			checkbox.setEnabled(false);
			break;
		}
		checkbox.setChecked(arg1.isChecked);
		checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				arg1.isChecked = isChecked;
				notifyDataSetChanged();
			}
		});
		tv_quxiao_dingdan.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				EventBus.getDefault().post(arg1, "cancaleOrder");
			}
		});
		tv_danhao.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				DialogUtils.showEditDanhaoDialog(false, context, arg1.order_sn,arg1.ordership.express_company_text,arg1.ordership.invoice_no, new onDialogDiamissListener() {

					@Override
					public void onDialogDismiss(String... strings) {
						fahuoAndEditDnhao(arg1, strings);
					}
				});
			}
		});
		tv_fahuo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				DialogUtils.showEditDanhaoDialog(true, context, arg1.order_sn,null,null, new onDialogDiamissListener() {

					@Override
					public void onDialogDismiss(String... strings) {
						fahuoAndEditDnhao(arg1, strings);
					}
				});
			}
		});
		tv_diaozhengfeiyong.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				EditPrivce(arg1);
			}
		});
		tv_shoudaohuokuan.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				receiverPrice(arg1);
			}
		});
		tv_wuliu.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
//				OpenBrowUtils.startBrower(context, );
				Map<String,String> map = HttpUtil.getParams("app","news","act","express","order_id",arg1.order_id,"user_id", UserUtils.getUserid(context)+"");
				Http.getInstant().sendRequest(context, map, new HttpListener() {
					@Override
					public void onHttpSuccess(String result) {
						JSONObject jsonObject = JSON.parseObject(result);
						jsonObject = jsonObject.getJSONObject("retval");
						String url = jsonObject.getString("url");
						OpenBrowUtils.startBrower(context,url,"查看物流");
					}

					@Override
					public void onHttpError(String error) {

					}
				});
			}
		});
	}

	private void receiverPrice(final StoreSingDanData arg1) {
		DialogUtils.showReceiverHuokuan(context, arg1.order_sn, new onDialogDiamissListener() {
			
			@Override
			public void onDialogDismiss(String... strings) {
				String content = strings[0];
				Map<String, String> map = HttpUtil.getParams("app","sellerorder","act","received_pay","order_id",arg1.order_id,"remark",content);
				Http.getInstant().sendRequest(context, map, new HttpListener() {
					
					@Override
					public void onHttpSuccess(String result) {
						JSONObject jsonObject = JSON.parseObject(result);
						Toast.makeText(context, jsonObject.getString("retval"), Toast.LENGTH_SHORT).show();
						EventBus.getDefault().post("", "refreshDing");
					}
					
					@Override
					public void onHttpError(String error) {

					}
				});
			}
		});
	}
	private void fahuoAndEditDnhao(final StoreSingDanData arg1, String... strings) {
		String danhao = strings[0];
		String yunyin = strings[1];
		String wuliu = strings[2];
		Map<String, String> map = HttpUtil.getParams("app", "sellerorder", "act", "shipped", "order_id", arg1.order_id,
				"express_company", wuliu, "invoice_no", danhao, "remark", yunyin);
		Http.getInstant().sendRequest(context, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				Toast.makeText(context, jsonObject.getString("retval"), Toast.LENGTH_SHORT).show();
				EventBus.getDefault().post("", "refreshDing");
			}

			@Override
			public void onHttpError(String error) {
			}
		});
	}

	/**
	 * 调整费用
	 * 
	 * @param arg1
	 */
	private void EditPrivce(final StoreSingDanData arg1) {
		DialogUtils.showEditPriceDialog(context, arg1.order_sn, Float.parseFloat(arg1.order_amount), Float.parseFloat(arg1.shipping_fee),
				new onDialogDiamissListener() {

					@Override
					public void onDialogDismiss(String... strings) {
						String shangpinPrice = strings[0];
						String peisongPrice = strings[1];
						Map<String, String> map = HttpUtil.getParams("app", "sellerorder", "act", "adjust_fee",
								"order_id", arg1.order_id, "shipping_fee", peisongPrice, "goods_amount", shangpinPrice);
						Http.getInstant().sendRequest(context, map, new HttpListener() {

							@Override
							public void onHttpSuccess(String result) {
								JSONObject jsonObject = JSON.parseObject(result);
								Toast.makeText(context, jsonObject.getString("retval"), Toast.LENGTH_SHORT).show();
								EventBus.getDefault().post("", "refreshDing");
							}

							@Override
							public void onHttpError(String error) {

							}
						});
					}
				});
	}
}
